package log

import (
	"testing"

	"github.com/Azure/go-autorest/autorest/mocks"
	elastic "github.com/elastic/go-elasticsearch/v6"
	"github.com/elastic/go-elasticsearch/v6/esapi"
	. "github.com/smartystreets/goconvey/convey"
)

func TestQueryEs(t *testing.T) {
	Convey("测试正常查询", t, func() {
		cfg := elastic.Config{
			Addresses: []string{
				"127.0.0.1",
			},
		}
		client, _ := elastic.NewClient(cfg)
		client.Search = func(...func(*esapi.SearchRequest)) (*esapi.Response, error) {
			return &esapi.Response{
				StatusCode: 200,
				Header:     nil,
				Body:       mocks.NewBody("{\n    \"_scroll_id\": \"DnF1ZXJ5VGhlbkZldGNoIQAAAAAAVyCkFl9Scnl4bEp6VGkyd1NVLWI3VEtOS3cAAAAAABIToBZsQVVrOGgtN1NvMmh0Ykx0Wk9NSnFBAAAAAABXIKUWX1JyeXhsSnpUaTJ3U1UtYjdUS05LdwAAAAAAEhOcFmxBVWs4aC03U28yaHRiTHRaT01KcUEAAAAAABITnRZsQVVrOGgtN1NvMmh0Ykx0Wk9NSnFBAAAAAAASE5gWbEFVazhoLTdTbzJodGJMdFpPTUpxQQAAAAAAEhOZFmxBVWs4aC03U28yaHRiTHRaT01KcUEAAAAAABIToRZsQVVrOGgtN1NvMmh0Ykx0Wk9NSnFBAAAAAAASE5sWbEFVazhoLTdTbzJodGJMdFpPTUpxQQAAAAAAEhOeFmxBVWs4aC03U28yaHRiTHRaT01KcUEAAAAAABITnxZsQVVrOGgtN1NvMmh0Ykx0Wk9NSnFBAAAAAAASE5oWbEFVazhoLTdTbzJodGJMdFpPTUpxQQAAAAAAVyCnFl9Scnl4bEp6VGkyd1NVLWI3VEtOS3cAAAAAABITohZsQVVrOGgtN1NvMmh0Ykx0Wk9NSnFBAAAAAAACb7AWcm5CMkIxWFlTOE9NdmI5VWI0LXdaUQAAAAAAVyCmFl9Scnl4bEp6VGkyd1NVLWI3VEtOS3cAAAAAAFcgqBZfUnJ5eGxKelRpMndTVS1iN1RLTkt3AAAAAAACb7EWcm5CMkIxWFlTOE9NdmI5VWI0LXdaUQAAAAAAAm-yFnJuQjJCMVhZUzhPTXZiOVViNC13WlEAAAAAAFcgqRZfUnJ5eGxKelRpMndTVS1iN1RLTkt3AAAAAAACb7MWcm5CMkIxWFlTOE9NdmI5VWI0LXdaUQAAAAAAAm-0FnJuQjJCMVhZUzhPTXZiOVViNC13WlEAAAAAABIToxZsQVVrOGgtN1NvMmh0Ykx0Wk9NSnFBAAAAAAASE6YWbEFVazhoLTdTbzJodGJMdFpPTUpxQQAAAAAAAm-1FnJuQjJCMVhZUzhPTXZiOVViNC13WlEAAAAAAFcgqhZfUnJ5eGxKelRpMndTVS1iN1RLTkt3AAAAAAASE6QWbEFVazhoLTdTbzJodGJMdFpPTUpxQQAAAAAAEhOlFmxBVWs4aC03U28yaHRiTHRaT01KcUEAAAAAAFcgqxZfUnJ5eGxKelRpMndTVS1iN1RLTkt3AAAAAABXIKwWX1JyeXhsSnpUaTJ3U1UtYjdUS05LdwAAAAAAAm-2FnJuQjJCMVhZUzhPTXZiOVViNC13WlEAAAAAAAJvtxZybkIyQjFYWVM4T012YjlVYjQtd1pRAAAAAABXIK0WX1JyeXhsSnpUaTJ3U1UtYjdUS05Ldw==\",\n    \"took\": 17,\n    \"timed_out\": false,\n    \"_shards\": {\n        \"total\": 33,\n        \"successful\": 33,\n        \"skipped\": 0,\n        \"failed\": 0\n    },\n    \"hits\": {\n        \"total\": 2,\n        \"max_score\": 7.9250135,\n        \"hits\": [\n            {\n                \"_index\": \"fcs-2020.08.28\",\n                \"_type\": \"fluentd\",\n                \"_id\": \"i_f_M3QBka4FrFukqJXr\",\n                \"_score\": 7.9250135,\n                \"_source\": {\n                    \"logger\": \"runtime\",\n                    \"ts\": \"2020-08-28T07:35:57Z\",\n                    \"requestId\": \"9013acff79f3448db4c3af5d6cc1c593\",\n                    \"log\": \"Hello world\",\n                    \"user_name\": \"inspurclouddev\",\n                    \"cluster_id\": \"8f861da0-6fbd-4470-862a-525fcee4b639\",\n                    \"cluster_name\": \"CKS-20200710165350-383\",\n                    \"Hostname\": \"inspurclouddev-bgxbhv9j8f-slave-1\",\n                    \"timestamp\": \"2020-08-28T07:35:57.812189347+0000\",\n                    \"long_time\": 1598600157812189347\n                }\n            },\n            {\n                \"_index\": \"fcs-2020.08.28\",\n                \"_type\": \"fluentd\",\n                \"_id\": \"jPf_M3QBka4FrFukqJXr\",\n                \"_score\": 7.9250135,\n                \"_source\": {\n                    \"logger\": \"runtime\",\n                    \"ts\": \"2020-08-28T07:35:57Z\",\n                    \"requestId\": \"9013acff79f3448db4c3af5d6cc1c593\",\n                    \"log\": \"\\n\",\n                    \"user_name\": \"inspurclouddev\",\n                    \"cluster_id\": \"8f861da0-6fbd-4470-862a-525fcee4b639\",\n                    \"cluster_name\": \"CKS-20200710165350-383\",\n                    \"Hostname\": \"inspurclouddev-bgxbhv9j8f-slave-1\",\n                    \"timestamp\": \"2020-08-28T07:35:57.812653961+0000\",\n                    \"long_time\": 1598600157812653961\n                }\n            }\n        ]\n    }\n}"),
			}, nil
		}
		client.Scroll = func(...func(*esapi.ScrollRequest)) (*esapi.Response, error) {
			return &esapi.Response{
				StatusCode: 200,
				Header:     nil,
				Body:       mocks.NewBody("{\n    \"_scroll_id\": \"DnF1ZXJ5VGhlbkZldGNoIQAAAAAAEYVKFmxBVWs4aC03U28yaHRiTHRaT01KcUEAAAAAABGFRRZsQVVrOGgtN1NvMmh0Ykx0Wk9NSnFBAAAAAAARhUkWbEFVazhoLTdTbzJodGJMdFpPTUpxQQAAAAAAEYVGFmxBVWs4aC03U28yaHRiTHRaT01KcUEAAAAAAFRQrxZfUnJ5eGxKelRpMndTVS1iN1RLTkt3AAAAAAAAB9wWcm5CMkIxWFlTOE9NdmI5VWI0LXdaUQAAAAAAVFCsFl9Scnl4bEp6VGkyd1NVLWI3VEtOS3cAAAAAAFRQrhZfUnJ5eGxKelRpMndTVS1iN1RLTkt3AAAAAABUUKsWX1JyeXhsSnpUaTJ3U1UtYjdUS05L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        \"successful\": 33,\n        \"skipped\": 0,\n        \"failed\": 0\n    },\n    \"hits\": {\n        \"total\": 0,\n        \"max_score\": null,\n        \"hits\": []\n    }\n}"),
			}, nil
		}
		_, err := QueryEs(client, "aaa")
		So(err, ShouldBeNil)
	})
	Convey("测试异常查询", t, func() {
		cfg := elastic.Config{
			Addresses: []string{
				"127.0.0.1",
			},
		}
		client, _ := elastic.NewClient(cfg)
		client.Search = func(...func(*esapi.SearchRequest)) (*esapi.Response, error) {
			return &esapi.Response{
				StatusCode: 404,
				Header:     nil,
				Body:       mocks.NewBody("{\n    \"_scroll_id\": \"DnF1ZXJ5VGhlbkZldGNoIQAAAAAAEYVKFmxBVWs4aC03U28yaHRiTHRaT01KcUEAAAAAABGFRRZsQVVrOGgtN1NvMmh0Ykx0Wk9NSnFBAAAAAAARhUkWbEFVazhoLTdTbzJodGJMdFpPTUpxQQAAAAAAEYVGFmxBVWs4aC03U28yaHRiTHRaT01KcUEAAAAAAFRQrxZfUnJ5eGxKelRpMndTVS1iN1RLTkt3AAAAAAAAB9wWcm5CMkIxWFlTOE9NdmI5VWI0LXdaUQAAAAAAVFCsFl9Scnl4bEp6VGkyd1NVLWI3VEtOS3cAAAAAAFRQrhZfUnJ5eGxKelRpMndTVS1iN1RLTkt3AAAAAABUUKsWX1JyeXhsSnpUaTJ3U1UtYjdUS05LdwAAAAAAVFCxFl9Scnl4bEp6VGkyd1NVLWI3VEtOS3cAAAAAAFRQrRZfUnJ5eGxKelRpMndTVS1iN1RLTkt3AAAAAABUULAWX1JyeXhsSnpUaTJ3U1UtYjdUS05LdwAAAAAAAAfeFnJuQjJCMVhZUzhPTXZiOVViNC13WlEAAAAAAAAH3RZybkIyQjFYWVM4T012YjlVYjQtd1pRAAAAAAARhUQWbEFVazhoLTdTbzJodGJMdFpPTUpxQQAAAAAAVFCyFl9Scnl4bEp6VGkyd1NVLWI3VEtOS3cAAAAAAFRQsxZfUnJ5eGxKelRpMndTVS1iN1RLTkt3AAAAAABUULQWX1JyeXhsSnpUaTJ3U1UtYjdUS05LdwAAAAAAEYVHFmxBVWs4aC03U28yaHRiTHRaT01KcUEAAAAAABGFSBZsQVVrOGgtN1NvMmh0Ykx0Wk9NSnFBAAAAAAARhUsWbEFVazhoLTdTbzJodGJMdFpPTUpxQQAAAAAAVFC1Fl9Scnl4bEp6VGkyd1NVLWI3VEtOS3cAAAAAAFRQtxZfUnJ5eGxKelRpMndTVS1iN1RLTkt3AAAAAAAAB-AWcm5CMkIxWFlTOE9NdmI5VWI0LXdaUQAAAAAAVFC4Fl9Scnl4bEp6VGkyd1NVLWI3VEtOS3cAAAAAABGFTBZsQVVrOGgtN1NvMmh0Ykx0Wk9NSnFBAAAAAAAAB98Wcm5CMkIxWFlTOE9NdmI5VWI0LXdaUQAAAAAAVFC2Fl9Scnl4bEp6VGkyd1NVLWI3VEtOS3cAAAAAABGFUBZsQVVrOGgtN1NvMmh0Ykx0Wk9NSnFBAAAAAABUULkWX1JyeXhsSnpUaTJ3U1UtYjdUS05LdwAAAAAAEYVOFmxBVWs4aC03U28yaHRiTHRaT01KcUEAAAAAABGFTRZsQVVrOGgtN1NvMmh0Ykx0Wk9NSnFBAAAAAAARhU8WbEFVazhoLTdTbzJodGJMdFpPTUpxQQ==\",\n    \"took\": 19,\n    \"timed_out\": false,\n    \"_shards\": {\n        \"total\": 33,\n        \"successful\": 33,\n        \"skipped\": 0,\n        \"failed\": 0\n    },\n    \"hits\": {\n        \"total\": 0,\n        \"max_score\": null,\n        \"hits\": []\n    }\n}"),
			}, nil
		}
		client.Scroll = func(...func(*esapi.ScrollRequest)) (*esapi.Response, error) {
			return &esapi.Response{
				StatusCode: 404,
				Header:     nil,
				Body:       mocks.NewBody("{\n    \"_scroll_id\": \"DnF1ZXJ5VGhlbkZldGNoIQAAAAAAEYVKFmxBVWs4aC03U28yaHRiTHRaT01KcUEAAAAAABGFRRZsQVVrOGgtN1NvMmh0Ykx0Wk9NSnFBAAAAAAARhUkWbEFVazhoLTdTbzJ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cAAAAAABGFUBZsQVVrOGgtN1NvMmh0Ykx0Wk9NSnFBAAAAAABUULkWX1JyeXhsSnpUaTJ3U1UtYjdUS05LdwAAAAAAEYVOFmxBVWs4aC03U28yaHRiTHRaT01KcUEAAAAAABGFTRZsQVVrOGgtN1NvMmh0Ykx0Wk9NSnFBAAAAAAARhU8WbEFVazhoLTdTbzJodGJMdFpPTUpxQQ==\",\n    \"took\": 19,\n    \"timed_out\": false,\n    \"_shards\": {\n        \"total\": 33,\n        \"successful\": 33,\n        \"skipped\": 0,\n        \"failed\": 0\n    },\n    \"hits\": {\n        \"total\": 0,\n        \"max_score\": null,\n        \"hits\": []\n    }\n}"),
			}, nil
		}
		_, err := QueryEs(client, "aaa")
		So(err, ShouldNotBeNil)
	})
}
